package cn.group.hotel2.controller;

import cn.group.hotel2.pojo.dto.RoleAddNewDTO;
import cn.group.hotel2.pojo.vo.RoleListItemVO;
import cn.group.hotel2.service.IRoleService;
import cn.group.hotel2.utils.PageResult;
import cn.group.hotel2.utils.PageUtil;
import cn.group.hotel2.web.JsonResult;
import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色控制器
 *
 * @author Edgar
 * @version 1.0.0
 * @date 2022/11/28
 */
@Slf4j
@Api(tags = "角色管理")
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @ApiOperation("新增角色")
    @ApiOperationSupport(order = 100)
    @PostMapping(value = "/add-new",produces = "application/json")
    public JsonResult<Void> addNew(String  jsonStr){
        //TODO参数为空
        log.debug("{}",jsonStr);
        RoleAddNewDTO roleAddNewDTO = JSONObject.parseObject(jsonStr, RoleAddNewDTO.class);
        roleService.addNew(roleAddNewDTO);
        return JsonResult.ok();
    }
    @ApiOperation("删除角色")
    @ApiOperationSupport(order = 200)
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<Void> deleteByRoleId(@PathVariable Long id){
        System.out.println(id);
        roleService.deleteByRoleId(id);
        return JsonResult.ok();
    }

    @ApiOperation("角色列表")
    @ApiOperationSupport(order = 400)
    @GetMapping("")
    public JsonResult<PageResult> list(int page,int limit){
        Map<String,Object> params = new HashMap<>();
        params.put("page",page);
        params.put("limit",limit);
        PageUtil pageUtil = new PageUtil(params);
        PageResult list = roleService.list(pageUtil);
        return JsonResult.ok(list);
    }
}
